/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.ConnectionClient;
import org.evswork.getwit.smackwrap.SmackWrapper;
import org.evswork.getwit.smackwrap.Talk;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

public class GttMainActivity extends Activity implements ConnectionClient{
	TitleBarWrapper tbWrapper;
	PagerCtlWrapper pctlWrapper;
	HomePageWrapper hpWrapper;
	TweetPageWrapper tpWrapper;
	EditPageWrapper edWrapper;
	MessageSender msgSender;
	
	private void loadPreferences() {
		SharedPreferences pref;
		pref = PreferenceManager.getDefaultSharedPreferences(this);
		if (
			pref.getString("user", "").length() == 0 || 
			pref.getString("password", "").length() == 0 || 
			pref.getString("bot", "").length() == 0) {
			startActivity(new Intent(this,GttUserActivity.class));
		}
	}
	
	private void bindView() {
		tbWrapper = new TitleBarWrapper(this);
		hpWrapper = new HomePageWrapper(this);
		tpWrapper = new TweetPageWrapper(this);
		pctlWrapper = new PagerCtlWrapper(this);
		edWrapper = new EditPageWrapper(this);
		msgSender = new MessageSender(this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		if (PreferenceManager
			.getDefaultSharedPreferences(this)
			.getString("bg_theme", "white")
			.equals("white"))
		{
			setTheme(android.R.style.Theme_Light);
		} else {
			setTheme(android.R.style.Theme_Black);
		}
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		bindView();
		loadPreferences();
		showHomeLayout();
		// try connect on create
		SmackWrapper.getInstance().setClient(this, this);
		Talk.send(PreferenceManager.getDefaultSharedPreferences(this).getString("req_on_launch", "-ho"));
	}
	
	public void showHomeLayout() {
		hpWrapper.show();
		tpWrapper.hide();
		edWrapper.hide();
	}
	
	public void showTweetLayout() {
		hpWrapper.hide();
		tpWrapper.show();
		edWrapper.hide();
	}
	
	public void showEditLayout(int status,boolean needref) {
		hpWrapper.hide();
		tpWrapper.hide();
		edWrapper.show(status,needref);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater= getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.pref_menu: 
			startActivity(new Intent(this,GttPrefActivity.class));
			return true;
		case R.id.page_menu:
			pctlWrapper.showPageDialog();
			return true;
		case R.id.regexp_menu:
			startActivity(new Intent(this,RegExpActivity.class));
			return true;
		case R.id.help_menu:
			startActivity(new Intent(this,HelpActivity.class));
			return true;
		case R.id.about_menu:
			new AlertDialog.Builder(this)
				.setIcon(R.drawable.icon_wbg)
				.setTitle(R.string.title)
				.setView(LayoutInflater.from(this).inflate(R.layout.about, null))
				.setPositiveButton("确定", null)
				.show();
			return true;
		case R.id.exit_menu:
			finish();
			return true;
		}
		return false;
	}
	
	public void onTweetSelected(TweetObject data) {
		if (data.flag != TweetObject.NOT_TWEET) {
			tpWrapper.update(data);
			showTweetLayout();
		} else {
			new UserOperationDlgWrapper(this,data);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) {
			if (tpWrapper.TweetLayout.getVisibility() == View.VISIBLE) {
				showHomeLayout();
				return true;
			}
			if (edWrapper.EditLayout.getVisibility() == View.VISIBLE)
			{
				showHomeLayout();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		SmackWrapper.getInstance().setClient(this, this);
		((TweetListAdapter)hpWrapper.MsgList.getAdapter()).notifyDataSetChanged();
		hpWrapper.notifyPreferenceChanged();
	}

	@Override
	public void notifyConnecting(boolean status) {
		if (status)
			tbWrapper.showProgressBar();
		else
			tbWrapper.hideProgressBar();
	}

	@Override
	public void processMessage(final String msg) {
		this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				((TweetListAdapter)hpWrapper.MsgList.getAdapter()).postRawMessage(msg);
				hpWrapper.MsgList.setSelection(0);
			}
			
		});
	}
}
